package com.example.wiki3.activaty;

import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wiki3.R;

public class RegisterActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);
        //初始化获取的布局元素
        find();
    }
    //获取注册按钮，预先定义，下方输入框同理
    private Button register;
    private EditText username,password,phone,emil;



    //控制注册按钮点击完后不给点击了
    boolean flag = false;

    //跳转回登录页面
    public void gobak(View view){
        Intent gl = new Intent(RegisterActivity.this, FirstActivity.class);
        startActivity(gl);
    }
    //初始化获取的布局元素
    public void find(){
        username = findViewById(R.id.rusername);
        password = findViewById(R.id.password_first);
        phone = findViewById(R.id.rpassword);
        emil = findViewById(R.id.remil);
        register = findViewById(R.id.mregister);
        register.setOnClickListener(this);
    }
    //注册逻辑实现
    public void register(){
        String ru = username.getText().toString();
        String rps = password.getText().toString();
        String rph = phone.getText().toString();
        String rem = emil.getText().toString();

        if (ru.isEmpty()) {
            Toast.makeText(this, "账号不能为空！", Toast.LENGTH_SHORT).show();
        } else if (rps.isEmpty()) {
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
        } else if (rph.isEmpty()) {
            Toast.makeText(this, "电话不能为空!", Toast.LENGTH_SHORT).show();
        }else if(rem.isEmpty()){
            Toast.makeText(this, "邮箱不能为空！", Toast.LENGTH_SHORT).show();
        }else{

            // 使用SharedPreferences存储用户信息
            SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("username", ru);
            editor.putString("password", rps);
            editor.putString("phone", rph);
            editor.putString("email", rem);
            editor.apply(); // 应用更改
            register.setEnabled(false);
            register.setTextColor(0xFFD0EFC6);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            // 注册成功后跳转到MainActivity
            Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
            startActivity(intent);
            finish(); // 关闭当前Activity
        }
    }

    //监听按钮点击事件
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.mregister) {
            register();
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}